package com.phoonsawat.andriod.sample.learnandriod;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button btnLaunch;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// view matching
		btnLaunch = (Button) findViewById(R.id.btnLaunch);
		btnLaunch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				// open new activity
				Intent intent = new Intent(MainActivity.this,
						SampleActivity.class);
				startActivity(intent);

				/*
				 * Toast msg = Toast.makeText(getBaseContext(),
				 * "You have clicked Button 2", Toast.LENGTH_LONG); msg.show();
				 */
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onBackPressed() {
		AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
		alert.setTitle(getResources().getString(R.string.confirm))
				.setIcon(getResources().getDrawable(R.drawable.confirm))
				.setMessage(getResources().getString(R.string.exit_application))
				.setNegativeButton(getResources().getString(R.string.cancel),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						})
				.setPositiveButton(getResources().getString(R.string.ok),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								MainActivity.this.finish();
							}
						}).create();

		alert.show();

	}

}
